package com.itcast.demo.simple;

import java.util.*;

/**
 * 实现一个特殊功能的栈，在实现栈的基本功能的基础上，再实现返回栈中最小元素的操作。
 *
 * @author zheng.zhang
 */
public class Test8 {
    static int i;

    private static class InnerClass {
        private static int i;

        public static void main(String[] args) {
            System.out.println(i);
        }
    }

    /**
     * 存放所有元素值
     */
    private static final Stack<Integer> STACK = new Stack<>();

    /**
     * 维护栈的最小元素
     */
    private static final Stack<Integer> MIN_STACK = new Stack<>();

    public static void main(String[] args) {
        Test8 test8 = new Test8();
        int[][] arr = {{1, 3}, {1, 2}, {1, 1}, {3}, {2}, {3}};
        int[] minStack = test8.getMinStack(arr);
        System.out.println(Arrays.toString(minStack));

        System.out.println(i);
        class InnerClass {

        }
    }

    /**
     * return a array which include all ans for op3
     *
     * @param op int整型二维数组 operator
     * @return int整型一维数组
     */
    public int[] getMinStack(int[][] op) {
        if (op == null || op.length == 0) {
            return null;
        }
        List<Integer> list = new ArrayList<>();
        for (int[] ints : op) {
            // 1.入栈
            if (ints[0] == 1) {
                push(ints[1]);
            }
            // 2.出栈
            if (ints[0] == 2) {
                pop();
            }
            // 3.获取最小值
            if (ints[0] == 3) {
                list.add(getMin());
            }
        }
        int[] arr = new int[list.size()];
        for (int i = 0; i < list.size(); i++) {
            arr[i] = list.get(i);
        }
        return arr;
    }

    /**
     * 压栈
     *
     * @param x 压栈元素
     */
    public void push(int x) {
        STACK.push(x);
        if (MIN_STACK.empty() || x <= MIN_STACK.peek()) {
            MIN_STACK.push(x);
        }
    }

    /**
     * 出栈
     */
    public void pop() {
        if (!STACK.empty()) {
            if (STACK.peek().equals(MIN_STACK.peek())) {
                MIN_STACK.pop();
            }
            STACK.pop();
        }
    }

    /**
     * 获取最小值
     *
     * @return 直接拿到栈2的栈顶元素
     */
    public int getMin() {
        return MIN_STACK.peek();
    }
}
